package com.screen.screendisplay.service.impl.electric;

import com.screen.screendisplay.entity.electric.ElectricOverviewItem;
import com.screen.screendisplay.mapper.electric.ElectricOverviewItemMapper;
import com.screen.screendisplay.service.electric.ElectricOverviewItemService;
import com.screen.screendisplay.vo.electric.ElectricOverviewDto;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * <p>
 * 火电产业总览信息表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class ElectricOverviewItemServiceImpl extends ServiceImpl<ElectricOverviewItemMapper, ElectricOverviewItem> implements ElectricOverviewItemService {

    /**
     * 获取火电产业总览统计数据
     * @param year 年份（必填）
     * @return 统计后的总览数据
     */
    @Override
    public ElectricOverviewDto getOverviewStatistics(Integer year) {
        // 构建查询条件
        LambdaQueryWrapper<ElectricOverviewItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ElectricOverviewItem::getYear, year);
        
        // 查询数据
        List<ElectricOverviewItem> dataList = this.list(queryWrapper);
        
        // 如果没有数据，返回空对象
        if (dataList.isEmpty()) {
            return new ElectricOverviewDto();
        }
        
        // 统计汇总数据
        ElectricOverviewDto dto = new ElectricOverviewDto();
        
        // 计算总装机容量总和
        BigDecimal totalInstalledCapacity = dataList.stream()
                .filter(item -> item.getTotalInstalledCapacity() != null)
                .map(ElectricOverviewItem::getTotalInstalledCapacity)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        dto.setTotalInstalledCapacity(totalInstalledCapacity);
        
        // 计算总负荷总和
        BigDecimal totalLoad = dataList.stream()
                .filter(item -> item.getTotalLoad() != null)
                .map(ElectricOverviewItem::getTotalLoad)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        dto.setTotalLoad(totalLoad);
        
        // 计算负荷率平均值
        BigDecimal loadRateSum = dataList.stream()
                .filter(item -> item.getLoadRate() != null)
                .map(ElectricOverviewItem::getLoadRate)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        long loadRateCount = dataList.stream()
                .filter(item -> item.getLoadRate() != null)
                .count();
        BigDecimal loadRate = loadRateCount > 0 ? 
                loadRateSum.divide(BigDecimal.valueOf(loadRateCount), 4, RoundingMode.HALF_UP) : 
                BigDecimal.ZERO;
        dto.setLoadRate(loadRate);
        
        // 计算总利用小时总和
        BigDecimal totalUtilizationHours = dataList.stream()
                .filter(item -> item.getTotalUtilizationHours() != null)
                .map(ElectricOverviewItem::getTotalUtilizationHours)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        dto.setTotalUtilizationHours(totalUtilizationHours);
        
        // 计算全员劳动生产率平均值
        BigDecimal laborProductivitySum = dataList.stream()
                .filter(item -> item.getLaborProductivity() != null)
                .map(ElectricOverviewItem::getLaborProductivity)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        long laborProductivityCount = dataList.stream()
                .filter(item -> item.getLaborProductivity() != null)
                .count();
        BigDecimal laborProductivity = laborProductivityCount > 0 ? 
                laborProductivitySum.divide(BigDecimal.valueOf(laborProductivityCount), 4, RoundingMode.HALF_UP) : 
                BigDecimal.ZERO;
        dto.setLaborProductivity(laborProductivity);
        
        // 计算日总发电量总和
        BigDecimal dailyGeneration = dataList.stream()
                .filter(item -> item.getDailyGeneration() != null)
                .map(ElectricOverviewItem::getDailyGeneration)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        dto.setDailyGeneration(dailyGeneration);
        
        // 计算日上网电量总和
        BigDecimal dailyGridSupply = dataList.stream()
                .filter(item -> item.getDailyGridSupply() != null)
                .map(ElectricOverviewItem::getDailyGridSupply)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        dto.setDailyGridSupply(dailyGridSupply);
        
        // 计算日供铝用电量总和
        BigDecimal dailyAluminumSupply = dataList.stream()
                .filter(item -> item.getDailyAluminumSupply() != null)
                .map(ElectricOverviewItem::getDailyAluminumSupply)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        dto.setDailyAluminumSupply(dailyAluminumSupply);
        
        // 计算日供热量总和
        BigDecimal dailyHeatingSupply = dataList.stream()
                .filter(item -> item.getDailyHeatingSupply() != null)
                .map(ElectricOverviewItem::getDailyHeatingSupply)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        dto.setDailyHeatingSupply(dailyHeatingSupply);
        
        // 计算安全生产天数总和
        Integer safeProductionDays = dataList.stream()
                .filter(item -> item.getSafeProductionDays() != null)
                .mapToInt(ElectricOverviewItem::getSafeProductionDays)
                .sum();
        dto.setSafeProductionDays(safeProductionDays);
        
        return dto;
    }

}
